package org.apache.ibatis.plugin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

@Intercepts({
       // @Signature(type = StatementHandler.class, method = "parameterize", args = java.sql.Statement.class),
       // @Signature(type = StatementHandler.class, method = "prepare", args = java.sql.Connection.class),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
})
public class MyPluginTest2 implements Interceptor {

    private static final Log log = LogFactory.getLog(MyPluginTest2.class);

    /**
     * 拦截目标对象的目标方法的执行；
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.error(this + " intercept method: 增强了方法" + invocation.getMethod());
        Object prObject = invocation.proceed();
        return prObject;
    }

    /**
     * 包装目标对象的：包装：为目标对象创建一个代理对象
     */
    @Override
    public Object plugin(Object target) {
        //获取包装对象，target的接口必须是签名的接口才能包装，否则返回对象本身
        return Plugin.wrap(target, this);
    }

    /**
     * setProperties：
     * 将插件注册时 的property属性设置进来
     */
    @Override
    public void setProperties(Properties properties) {
        log.warn(this + " setProperties method: prop = " + properties);
    }


}
